<?php
require "config/lib.php";

use app\admin\classes\AdminUser;
use core\G;
use core\modules\AcManager;
use core\Func;

// 登录验证
trait Auth{

  // 检测登录
  protected function Auth_Login(){
    // echo session('user');
    $user=AdminUser::getInstance();

    if(!$user->check()){
      // $this->error('您尚未登录系统',url('login/index/index'));
      $this->redirect('admin/login/index');
    }
    $this->user=$user;


  }
  // 检测权限
  protected function Auth_AC(){
    $hash=Func::getHash();
    $is_allow=$this->user->ac->check($hash);

    $allow_hash=[
      "/admin/index",
    ];

    if (in_array($hash,$allow_hash)) $is_allow=true;
    if (!$is_allow){
      $this->error("没有权限");
    }

  }

}


// 每个页面都包含的基础功能
trait BaseFunction{

  protected function BaseFunction(){

  }
  // 设置wapper
  protected function wapper($conf){
    G::set("Wapper_settings",$conf);
    $this->VD(["wapper"=>$conf]);
  }
  protected function menuActive($href){
    G::set("menuActive",$href);
  }
}

/**
 * 公共控制器
 */
class C extends think\Controller {

  use Auth;
  use BaseFunction;

  protected $viewData=[];
  static $hash="";

  protected $beforeActionList = [
        'Auth_Login' =>  [],
        'Auth_AC' =>  [],
        'BaseFunction'=> [],
    ];

  // 前置方法
  protected function initialize(){
    if (empty(static::$hash)) static::$hash=Func::getHash();
  }
  protected function VD($arr=[]){
    if (!empty($arr)) $this->viewData=array_merge($this->viewData,$arr);
    return $this->viewData;
  }
  protected function getVD($key,$defaultValue=""){
    if (isset($this->viewData[$key])){
      return $this->viewData[$key];
    }else{
      return $defaultValue;
    }

  }
  protected function VueData($data){
    return $this->VD(["vue_data"=>json_encode($data)]);
  }

}


/**
 * 公共模型
 */
class M extends think\Model {

  use think\model\concern\SoftDelete;

  protected $autoWriteTimestamp = 'datetime';

  protected $deleteTime = 'delete_at';
  protected $createTime = 'create_at';
  protected $updateTime = 'update_at';

  public function searchStatusAttr($query, $value, $data){
    if ($value!="")
    $query->where('status','=', $value);
  }
  public function searchNameAttr($query, $value, $data){
    if (!empty($value))
    $query->where('name','like', '%' . $value . '%');
  }
  public function searchDesAttr($query, $value, $data){
    if (!empty($value))
    $query->where('des','like', '%' . $value . '%');
  }
  public function searchRemarkAttr($query, $value, $data){
    if (!empty($value))
    $query->where('remark','like', '%' . $value . '%');
  }



}
